package com.situ.company.web220111.department.servlet;

import com.situ.company.web220111.department.model.DepartmentModel;
import com.situ.company.web220111.department.service.IDepartmentService;
import com.situ.company.web220111.department.service.impl.DepartmentServiceImpl;
import com.situ.company.web220111.util.FmtRequest;
import com.sun.jmx.snmp.SnmpString;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "DepartmentServlet", value = "/DepartmentServlet")
public class DepartmentServlet extends HttpServlet {

    private IDepartmentService service = new DepartmentServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Object res = null;
        switch (request.getParameter("action")) {
            case "list":
                res = list(request);
                break;
            case "page":
                res = page(request);
                break;
            case "add":
                res = add(request);
                break;
            case "del":
                res = del(request);
                break;
            case "get":
                res = get(request);
                break;
            case "upd":
                res = upd(request);
                break;
        }
        FmtRequest.write(response.getWriter(),res);
    }

    private Object page(HttpServletRequest request) {
        String code = request.getParameter("code");
        String name = request.getParameter("name");
        String pageIndex = request.getParameter("pageIndex");
        String pageLimit = request.getParameter("pageLimit");
        DepartmentModel model = new DepartmentModel();
        model.setCode(code);
        model.setName(name);
        model.setPageIndex(Integer.parseInt(pageIndex));
        model.setPageLimit(Integer.parseInt(pageLimit));
        model.setPageOn(true);
        List<DepartmentModel> list =  service.selectList(model); // 带有查询条件和分页条件，得到的当前页面要显示的数据
        Integer count = service.selectCount(model);  // 带有查询条件，不带有分页条件，得到的记录条数
        Map<String,Object> map = new HashMap<>();
        map.put("list",list);
        map.put("count",count);
        return map;
    }

    private String upd(HttpServletRequest request) {
        return service.updateAll(FmtRequest.parseModel(request,DepartmentModel.class));
    }

    private Object get(HttpServletRequest request) {
        return service.selectModel(FmtRequest.parseModel(request,DepartmentModel.class));
    }

    private String del(HttpServletRequest request) {
        String code = request.getParameter("code");
        return service.delete(new DepartmentModel(code));
        //return service.delete(FmtRequest.parseModel(request,DepartmentModel.class));
    }

    private String add(HttpServletRequest request) {
        // 把得到的参数依次映射到实体类中
        DepartmentModel model =  FmtRequest.parseModel(request, DepartmentModel.class);
        return service.insert(model);
    }

    private List<DepartmentModel> list(HttpServletRequest request) {
        DepartmentModel model =  FmtRequest.parseModel(request, DepartmentModel.class);
        return service.selectList(model);
    }
}
